package sam.classe;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class User extends Activity implements View.OnClickListener{
	
	private Button valider;
	private Button retour;
	
	private EditText pseudo ;
	private EditText mail;
	private EditText paswd;
	private EditText comfirmation;
	private EditText nom;
	private EditText prenom;
	private RadioButton oui;
	private RadioButton non;
	
	private String reponse;

	 HttpPost httppost;
	 HttpClient httpclient;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.inscription);
		
		valider = (Button)findViewById(R.id.vvalider);
		retour = (Button)findViewById(R.id.annuler);
		
		oui = (RadioButton)findViewById(R.id.oui);
		non = (RadioButton)findViewById(R.id.non);
		
		pseudo = (EditText)findViewById(R.id.editPseudo);
		mail = (EditText)findViewById(R.id.adresseM);
		paswd = (EditText)findViewById(R.id.editPass);
		comfirmation = (EditText)findViewById(R.id.EditPass2);
		nom = (EditText)findViewById(R.id.nomS);
		prenom = (EditText)findViewById(R.id.prenomS);
		
		valider.setOnClickListener(this);
		retour.setOnClickListener(this);
		
		non.setOnClickListener(this);
		oui.setOnClickListener(this);
	
		
	}

	
	
	
	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}






	@Override
	public void onClick(View v) {
		
		switch(v.getId()){
		
			case R.id.annuler:
				finish();
			break;
			
			case R.id.vvalider:
				
				httppost = new HttpPost("http://samsam0246813579.livehost.fr/sam/AjouterUser.php");
				
				final String N = nom.getText().toString(); 
				final String P = prenom.getText().toString(); 
				final String M = mail.getText().toString(); 
				final String Ps = pseudo.getText().toString(); 
				final String Pa = paswd.getText().toString();
				
				
		        httpclient = new DefaultHttpClient();
		        
		        

		        try
				{
		        	
		        	ArrayList<NameValuePair> queryParams = new ArrayList<NameValuePair>();
			        
			        queryParams.add(new BasicNameValuePair("NOM_USER", N));
			        queryParams.add(new BasicNameValuePair("PRENOM_USER", P ));
			        queryParams.add(new BasicNameValuePair("MAIL_USER", M));
			        queryParams.add(new BasicNameValuePair("LOGIN_USER", Ps ));
			        queryParams.add(new BasicNameValuePair("MDP_USER", Pa ));
		        	
					httppost.setEntity(new UrlEncodedFormEntity(queryParams));
					HttpResponse response = httpclient.execute(httppost);

					
					this.reponse = EntityUtils.toString(response.getEntity());
					Log.i("postData", response.getStatusLine().toString());
				}catch(UnsupportedEncodingException e)
				{
					Log.e("e", e.toString());
				}catch(ParseException e)
				{
					Log.e("e", e.toString());
					
				}
		        catch(ClientProtocolException e)
		        {
		        	Log.e("e", e.toString());
		        }catch(IOException e)
		        {
		        	Log.e("e", e.toString());
		        }
			
		        Intent coco = new Intent(User.this, Connection.class);
				startActivity(coco);
			break;
			
		}
	}

}
